package cn.happy.favdish.view.fragments

import android.app.Application
import android.opengl.Visibility
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import cn.happy.favdish.R
import cn.happy.favdish.application.FavDishApplication
import cn.happy.favdish.databinding.FragmentFavoriteDishesBinding
import cn.happy.favdish.model.entities.FavDish
import cn.happy.favdish.view.activities.MainActivity
import cn.happy.favdish.view.adapter.FavDishAdapter
import cn.happy.favdish.view.viewmodel.DashboardViewModel
import cn.happy.favdish.view.viewmodel.FavDishViewModel
import cn.happy.favdish.view.viewmodel.FavDishViewModelFactory

class FavoriteDishesFragment : Fragment() {

    private var _binding: FragmentFavoriteDishesBinding? = null


    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    private val mFavDishViewModel:FavDishViewModel by viewModels {
        FavDishViewModelFactory((requireActivity().application as FavDishApplication).repository )
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = FragmentFavoriteDishesBinding.inflate(inflater, container, false)

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mFavDishViewModel.favoriteDishList.observe(viewLifecycleOwner){
            dishes ->
            dishes.let{
                binding.rvFavoriteList.layoutManager = GridLayoutManager(requireActivity(),2)
                val adapter = FavDishAdapter(this)
                binding.rvFavoriteList.adapter = adapter

                if(it.isNotEmpty()){
                    binding.tvNoFavoriteDishes.visibility = View.GONE
                    binding.rvFavoriteList.visibility = View.VISIBLE
                    adapter.dishesList(it)

                }else{
                    binding.tvNoFavoriteDishes.visibility = View.VISIBLE
                    binding.rvFavoriteList.visibility = View.GONE
                }

            }
        }
    }

    fun dishDetails(favDish: FavDish){
        val bundle = Bundle()
        bundle.putParcelable("fav_dish",favDish)
        findNavController().navigate(R.id.action_navigation_favorite_to_dishDetailsFragment,bundle)

        if(requireActivity() is MainActivity){
            (activity as MainActivity).hideBottomNavigationView()
        }

    }

    override fun onResume() {
        super.onResume()
        if(requireActivity() is MainActivity){
            (activity as MainActivity?)?.showBottomNavigationView()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}